package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.itheima.constant.RedisConst;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author mengwei
 * @date 2019/8/13
 * @desc
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    JedisPool jedisPool;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);
    }

    @Override
    @Transactional
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        // 清空redis 中存储的套餐数据
        Jedis jedis = jedisPool.getResource();
        jedis.del("setmealList");


        //添加套餐
        setmealDao.add(setmeal);
        //维护中间表数据
        if (setmeal.getId() != null && checkgroupIds != null
                && checkgroupIds.length > 0){
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.set(setmeal.getId(),checkgroupId);
            }
        } else {
            throw new RuntimeException("套餐和检查组中间表维护失败");
        }
        //图片名称已存储到数据库，保存图片名称到redis中
        jedisPool.getResource().sadd(RedisConst.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public List<Setmeal> findAll() {
        // 1 从jedis 中获取套餐信息
        Jedis jedis = jedisPool.getResource();
        String setmealList = jedis.get("setmealList");
        // 2 判断
        if(setmealList==null){
            List<Setmeal> list = setmealDao.findAll();
            jedis.set("setmealList", JSONArray.toJSONString(list));
            return list;
        }else {
            List<Setmeal> list = JSONArray.parseArray(setmealList, Setmeal.class);
            System.out.println(list);
            return list;
        }

    }

    @Override
    public Setmeal findById(Integer id) {
        // redis 中获取 套餐数据
        String setmealJson = jedisPool.getResource().get("setmeal"+id);
        // 判断
        if(setmealJson==null){
            Setmeal setmeal = setmealDao.findById(id);
            jedisPool.getResource().set("setmeal"+id,JSONObject.toJSONString(setmeal));
            return setmeal;
        }else {
            Setmeal setmeal = JSONObject.parseObject(setmealJson, Setmeal.class);
            return setmeal;
        }


    }

    @Override
    public Map<String, Object> getSetmealReport() {
        List<Map<String,Object>> mapList = setmealDao.getSetmealReport();
        List<String> setmealNames = new ArrayList<>();
        for (Map<String, Object> map : mapList) {
            String setmealName = String.valueOf(map.get("name"));
            setmealNames.add(setmealName);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("setmealNames",setmealNames);
        resultMap.put("setmealCount",mapList);
        return resultMap;
    }
}
